package kr.co.hk.date;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import kr.co.hk.R;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Toast;

public class DatePlaceDetail extends Activity {
	final Activity activity = this;
	private WebView webView;
	private ProgressDialog progressDialog;
	final private int PROGRESS_DIALOG = 0;
	private String url = "";
	private boolean gubun = false;
	private String html = "";
	private boolean pageEx = false;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.date_detail);

		Bundle extra = getIntent().getExtras();
		url = extra.getString("url");
		gubun = extra.getBoolean("gubun");

		webView = (WebView) findViewById(R.id.dateWebView);
		webView.clearCache(true);
		webView.getSettings().setSaveFormData(false);
		webView.getSettings().setBuiltInZoomControls(true);

		mainProcessing();
		
	}

	private Handler chdHandler = new Handler();

	private void mainProcessing() {
		pageEx = false;
		showDialog(PROGRESS_DIALOG);
		Thread childThread = new Thread(null, dobackgroundThreadProcessing,
				"Backgroud");
		childThread.start();
	}

	private Runnable dobackgroundThreadProcessing = new Runnable() {

		@Override
		public void run() {
			backgroudnThreadProcessing();
		}
	};

	private void backgroudnThreadProcessing() {
		getHtml(url);
		chdHandler.post(doUpdateGUI);
	}

	private Runnable doUpdateGUI = new Runnable() {

		@Override
		public void run() {
			
			updateGUI();
			
		}
	};

	private void updateGUI() {
		if (pageEx) {
			excePro();
		} else {
			webView.loadDataWithBaseURL(url, html, "text/html", "utf-8", null);
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		progressDialog.dismiss();
		DatePlaceDetail.this.removeDialog(PROGRESS_DIALOG);
		
		toast("네트워크 상황 및  이미지 크기에 따라 \n페이지 로딩 시간이 오래 걸릴 수 있습니다.");

	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case PROGRESS_DIALOG:

			progressDialog = new ProgressDialog(this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setMessage("Now loading..");
			progressDialog.setIndeterminate(true);
			progressDialog.setCancelable(false);
			return progressDialog;

		}
		return null;

	}

	private void getHtml(String addr) {
		StringBuilder body = new StringBuilder();
		try {
			URL url = new URL(addr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			if (conn != null) {
				conn.setConnectTimeout(10000);
				conn.setUseCaches(false);
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					BufferedReader br = new BufferedReader(
							new InputStreamReader(conn.getInputStream(),
									"utf-8"));
					String line = "";

					boolean bodyChk = false;

					while ((line = br.readLine()) != null) {
						if (gubun) {
							if (line.contains("divcolMap")) {
								bodyChk = true;
							}
							if (line.contains("<div class=\"col_map_tit\">")) {
								bodyChk = false;
								break;
							}

						} else {

							if (line.contains("<div class=\"col_review\">")) {
								bodyChk = true;
							}
							if (line.contains("<!-- /col_review -->")) {
								bodyChk = false;
								break;
							}

						}

						if (bodyChk) {
							body.append(line);
						}

					}
					String css = "<link href=\"http://l.yimg.com/ne/ykc/yui/reset-fonts-grids.css\" type=\"text/css\" rel=\"stylesheet\">";
					html = css + body.toString();

					br.close();
				}
				conn.disconnect();
			}
		} catch (Exception ex) {
			pageEx = true;
			ex.printStackTrace();
		}

	}

	private void excePro() {
		toast("페이지 로딩시 오류가  있습니다. \n 네트워크 확인 후 다시 시도 하세요!");
		onBackPressed();
	}

	private void toast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
}
